package com.ytktt.study.week12.task7;

import com.ytktt.study.week12.task7.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * TODO
 *
 * @author ytk
 */
@Slf4j
@SpringBootApplication
public class ConsumerApplication implements ApplicationRunner {

    @Autowired
    private OrderService orderService;

    @Value("${taskRegion}")
    private int taskRegion;

    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        ExecutorService executorService = Executors.newFixedThreadPool(taskRegion);
        for (int i = 1; i <= taskRegion; i++) {
            int region = i;
            executorService.submit(() -> {
                log.info("start region {}", region);
                for (;;) {
                    this.orderService.batchUpdateOrderStatus(region);
                    Thread.sleep(100L);
                }
            });
        }
    }
}
